package com.wanmait.community.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.community.entity.Account;
import com.wanmait.community.entity.Room;
import com.wanmait.community.mapper.*;
import com.wanmait.community.service.RoomService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements RoomService{
    @Resource
    private RoomMapper roomMapper;
    @Resource
    private BuildingMapper buildingMapper;
    @Resource
    private UnitMapper unitMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private AccountMapper accountMapper;
    @Override
    public Room findRoomByBIdAndUIdAndRoomName(Room room) {
        return roomMapper.findRoomByBIdAndUIdAndRoomName(room);
    }

    @Override
    public PageInfo<Room> findPage(Room room, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        LambdaQueryWrapper<Room> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(room.getBuildingId() != null, Room::getBuildingId, room.getBuildingId());
        queryWrapper.eq(room.getUnitId() != null, Room::getUnitId, room.getUnitId());
        List<Room> roomList = roomMapper.selectList(queryWrapper);
        roomList.forEach(room1 -> {
            room1.setBuilding(buildingMapper.selectById(room1.getBuildingId()));
            room1.setUnit(unitMapper.selectById(room1.getUnitId()));
            room1.setOwner(userMapper.selectById(room1.getOwnerId()));
        });
        return new PageInfo<>(roomList,9);
    }

    @Override
    public void deleteRoomById(Room room) {
        roomMapper.deleteRoomById(room);
        accountMapper.deleteByRoomId(room);
    }

    @Override
    public List<Room> findRoomByBuildingId(Integer buildingId) {
        LambdaQueryWrapper<Room> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(buildingId != null, Room::getBuildingId, buildingId);
        List<Room> roomList = roomMapper.selectList(queryWrapper);
        return roomList;
    }

    @Override
    public boolean insertRoom(List<Room> rooms) {
       for (Room room : rooms) {
            int roomResult = roomMapper.insertSelective(room);
            if (roomResult != 1) {
                return false;
            }
            int accountResult = accountMapper.insertSelective(new Account().setRoomId(room.getId())
                    .setBalance(new BigDecimal(0))
                    .setCreatBy(room.getCreatBy())
                    .setUpdateBy(room.getUpdateBy()));
            if (accountResult != 1) {
                return false;
            }
       }
        return true;
    }

}
